package com.qq.mobilemanager.view;

import com.qq.mobilesafe.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ToggleButton extends View {

	private Bitmap switchBitmap;// 滑动开关的背景图片
	private Bitmap slidBitmap;// 滑动块的背景图片
	private Bitmap offBitmap;
	private boolean currentState;
	private int currentX;// 手指触摸点的X 值
	private boolean isTouching = false;
	private ToggleBtnStateChangeListener mToggleBtnStateChangeListener;// 状态改变监听器

	public ToggleButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleButton);
		currentState = a.getBoolean(R.styleable.ToggleButton_CurrentState, false);
		int switchBtnBackgroudId = a.getInt(R.styleable.ToggleButton_SwitchBtnBackgroud, -1);
		int slidBtnBackgroudId = a.getInt(R.styleable.ToggleButton_SwitchBtnBackgroud, -1);
		int offBtnBackgroudId = a.getInt(R.styleable.ToggleButton_SwitchBtnBackgroud, -1);
		
		/*currentState = attrs.getAttributeBooleanValue(namespace,
				"CurrentState", false);
		int switchBtnBackgroudId = attrs.getAttributeResourceValue(namespace,
				"SwitchBtnBackgroud", -1);
		int slidBtnBackgroudId = attrs.getAttributeResourceValue(namespace,
				"SlidBtnBackgroud", -1);
		int offBtnBackgroudId = attrs.getAttributeResourceValue(namespace,
				"offBtnBackgroud", -1);*/

		setSwitchBtnBackgroudResource(switchBtnBackgroudId);
		setSlidBtnBackgroudResource(slidBtnBackgroudId);
		setBtnBackgroudResource(offBtnBackgroudId);
	}

	public ToggleButton(Context context) {
		super(context);
	}

	// 设置滑动开关的背景图片
	public void setSwitchBtnBackgroudResource(int switchBackground) {
		switchBitmap = BitmapFactory.decodeResource(getResources(),
				switchBackground);
	}

	public void setBtnBackgroudResource(int switchBackground) {
		offBitmap = BitmapFactory.decodeResource(getResources(),
				switchBackground);
	}

	// 为了可以高度自定义和增强可扩展性，我们可以给其创建一个自定义控件底部背景了一个方法
	// 设置滑动块的背景图片
	public void setSlidBtnBackgroudResource(int slideButtonBackground) {
		slidBitmap = BitmapFactory.decodeResource(getResources(),
				slideButtonBackground);
	}

	// 设置滑动开关的默认状态
	public void setCurrentState(boolean b) {
		currentState = b;
	}

	// 1、测量滑动开关的宽高
	// 测量控件的宽高
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(switchBitmap.getWidth(), switchBitmap.getHeight());
	}

	// 2、绘制，画出我们的滑动开关
	// canvas:画布，将图形绘制在canvas，才能显示到屏幕上
	@Override
	protected void onDraw(Canvas canvas) {

		if (currentState) {
			// 绘制滑动开关的背景图片
			canvas.drawBitmap(switchBitmap, 0, 0, null);
		} else {
			canvas.drawBitmap(offBitmap, 0, 0, null);
		}

		// 绘制滑动块的背景图片
		if (isTouching) {// 手指触摸的时候，根据currentx 的值来绘制滑动块
			// 根据手指的X 值，来绘制滑动块图片
			int left = currentX - slidBitmap.getWidth() / 2;
			if (left < 0) {// 设置左边界
				left = 0;
			} else if (left > (switchBitmap.getWidth() - slidBitmap.getWidth())) {// 设置右边界

				left = switchBitmap.getWidth() - slidBitmap.getWidth();
			}
			canvas.drawBitmap(slidBitmap, left, 0, null);
		} else { // 手指离开控件的时候，根据状态来绘制滑动块
			// 根据状态值，来绘制滑动块
			if (currentState) { // 当前为true，开关打开，滑动块显示在最右边
				canvas.drawBitmap(slidBitmap, switchBitmap.getWidth()
						- slidBitmap.getWidth(), 0, null);
			} else {// 当前为false，开关关闭，滑动块显示在最左边
				canvas.drawBitmap(slidBitmap, 0, 0, null);
			}
		}
	}

	// 当控件被触摸后，会调用该方法
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 当滑动块中心点大于滑动开关背景图片的中心线时，显示到右边，当前状态为true
		int center = switchBitmap.getWidth() / 2;

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:// 手指按下
			isTouching = true;
			currentX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:// 手指滑动
			isTouching = true;
			currentX = (int) event.getX();
			currentState = currentX > center;
			invalidate();
			break;
		case MotionEvent.ACTION_UP:// 手指抬起
			isTouching = false;
			currentX = (int) event.getX();
			boolean state = currentState;
			currentState = currentX > center;
			invalidate(); // 强制让控件重新绘制，ondraw；在滑动的时候调用ondraw方法
			if (mToggleBtnStateChangeListener != null && state != currentState) {
				mToggleBtnStateChangeListener
						.onToggleBtnStateChange(currentState);
			}
			break;
		}

		return true; // 自己处理触摸事件
	}

	public void setToggleBtnStateChangeListener(
			ToggleBtnStateChangeListener listener) {

		this.mToggleBtnStateChangeListener = listener;
	}

	// 定义滑动开关状态改变的回调接口
	public interface ToggleBtnStateChangeListener {
		void onToggleBtnStateChange(boolean currentState);
	}

}
